package com.wu.service.schedule;

import com.wu.pojo.po.paperPo.Paper;
import com.wu.service.PaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * [定时任务]
 *
 * @Author : [wuzhengru]
 * @Version : [v1.0]
 * @Date : [2021/5/25 17:31]
 */
@Slf4j
@Component
public class ScheduleTask {

    @Autowired
    private PaperService paperService;

    /**
     * 每天零点更新问卷状态
     */
    //    @Scheduled(fixedRate = 10000)
    @Scheduled(cron = "0 0 0 * * ?")
    public void testSchedule() {
        // 遍历问卷，将今天过期问卷状态设置为2-已结束
        List<Paper> papers = paperService.queryPaper();
        for (Paper paper : papers) {
            Date endTime = paper.getEndTime();
            if (null == endTime) {
                continue;
            }
            if (endTime.before(new Date())) {
                paperService.updatePaper(new Paper().setStatus("2"));
                log.info("定时任务-修改问卷{}(id={})状态成功",paper.getTitle(),paper.getPaperId());
            }
        }
    }

}
